#include "StdAfx.h"
#include "Keyer.h"
#include "Picture.h"
#include "graphlibHTW.h"
#include "Constants.h"
#include <algorithm>

// default ctor, initializes with keyingcolor black
Keyer::Keyer(void)
{
	keyingColor[0] = 0;
	keyingColor[1] = 0;
	keyingColor[2] = 0;
	keyingTolerance = 0;
}

Keyer::~Keyer(void)
{
}

unsigned char *Keyer::getKeyingColor() 
{
	return keyingColor;
}

void Keyer::setKeyingColor(unsigned char _red, unsigned char _green, unsigned char _blue)
{
	keyingColor[0] = _red;
	keyingColor[1] = _green;
	keyingColor[2] = _blue;
}

unsigned char Keyer::getKeyingTolerance()
{
	return keyingTolerance;
}

void Keyer::setKeyingTolerance(unsigned char _keyingTolerance)
{
	keyingTolerance = _keyingTolerance;
}


Picture *Keyer::keyPictures(Picture *background, Picture *foreground) 
{
		unsigned char *pic;
		Picture * pictureResult;
		int minWidth = min(background->pWidth,foreground->pWidth);
		int minHeight = min(background->pHeight,foreground->pHeight);
		int maxWidth = max(background->pWidth,foreground->pWidth);
		int maxHeight = max(background->pHeight,foreground->pHeight);
		int lineOffset = 0;
//		int resPixelWidth = minWidth*3;

		int pixelCount;
		int checkPictureCode = checkPictures(background,foreground);

		pic = (unsigned char *)htwCreateImage(FILENAME_PLACEHOLDER, minWidth, minHeight, 3);
		pictureResult = new Picture(pic,minWidth,minHeight,3);
		pixelCount = minWidth*minHeight*3;

		for (int i = 0; i<pixelCount;i=i+3)
		{

			if (checkPictureCode != SIZE_OK)
			{
				if (i%minWidth==0) 
					lineOffset += 3*(maxWidth - minWidth);
			}			

			//FOREGROUND BIGGER
			if (checkPictureCode == FOREGROUND_BIGGER)
			{
				if (((foreground->pic[i+lineOffset] >= keyingColor[2]-keyingTolerance) &&
					(foreground->pic[i+lineOffset] <= keyingColor[2]+keyingTolerance)) &&
					((foreground->pic[i+1+lineOffset] >= keyingColor[1]-keyingTolerance) &&
					(foreground->pic[i+1+lineOffset] <= keyingColor[1]+keyingTolerance)) &&
					((foreground->pic[i+2+lineOffset] >= keyingColor[0]-keyingTolerance) && 
					(foreground->pic[i+2+lineOffset] <= keyingColor[0]+keyingTolerance)))
				{						
					pictureResult->pic[i] = background->pic[i];
					pictureResult->pic[i+1] = background->pic[i+1];
					pictureResult->pic[i+2] = background->pic[i+2];
				}
				else
				{
					pictureResult->pic[i] = foreground->pic[i+lineOffset];
					pictureResult->pic[i+1] = foreground->pic[i+1+lineOffset];
					pictureResult->pic[i+2] = foreground->pic[i+2+lineOffset];
				}
			}
			//BACKGROUND BIGGER
			else
			{
				if (((foreground->pic[i] >= keyingColor[2]-keyingTolerance) &&
					(foreground->pic[i] <= keyingColor[2]+keyingTolerance)) &&
					((foreground->pic[i+1] >= keyingColor[1]-keyingTolerance) &&
					(foreground->pic[i+1] <= keyingColor[1]+keyingTolerance)) &&
					((foreground->pic[i+2] >= keyingColor[0]-keyingTolerance) && 
					(foreground->pic[i+2] <= keyingColor[0]+keyingTolerance)))
				{						
					pictureResult->pic[i] = background->pic[i+lineOffset];
					pictureResult->pic[i+1] = background->pic[i+1+lineOffset];
					pictureResult->pic[i+2] = background->pic[i+2+lineOffset];
				}
				else
				{
					pictureResult->pic[i] = foreground->pic[i];
					pictureResult->pic[i+1] = foreground->pic[i+1];
					pictureResult->pic[i+2] = foreground->pic[i+2];
				}
			}
		}
		return pictureResult;
}

/// proves fore- and backgroundpicture about proportional relation
int Keyer::checkPictures(Picture *background, Picture *foreground)
{
	if ((background->pWidth == foreground->pWidth) &&      
		(background->pHeight == foreground->pHeight))
	{
		return SIZE_OK;
	}
	else if ((background->pWidth >= foreground->pWidth) &&
		     (background->pHeight >= foreground->pHeight))
	{
		return BACKGROUND_BIGGER;
	}
	else if ((background->pWidth <= foreground->pWidth) &&
		     (background->pHeight <= foreground->pHeight))
	{
		return FOREGROUND_BIGGER;
	}
	else if (((background->pWidth >= foreground->pWidth) && (background->pHeight <= foreground->pHeight)) ||
			 ((background->pWidth <= foreground->pWidth) && (background->pHeight >= foreground->pHeight)))
	{
		return DIFFERENT_PROPORTIONS;
	}
}